package com.ming.liteflow.core.el;

import com.ming.common.beetl.util.StrUtil;
import com.ming.liteflow.core.el.bus.*;
import com.ming.liteflow.core.node.NodeInfo;
import com.yomahub.liteflow.builder.el.ELWrapper;

public class NodeInfoToELUtil {

    public static ELWrapper toELWrapper(NodeInfo info){
        handler(info);
        ELWrapper el = null;
        switch (info.getType()){
            case "common": el = ELBusThen.NEW().node(info).toELWrapper();break;
            case "switch": el = ELBusSwitch.NEW().node(info).toELWrapper();break;
            case "if": el = ELBusIf.NEW().node(info).toELWrapper();break;
            case "for": el = ELBusFor.NEW().node(info).toELWrapper();break;
            case "while": el = ELBusWhile.NEW().node(info).toELWrapper();break;
            case "iterator": el = ELBusIterator.NEW().node(info).toELWrapper();break;
            case "break": el = ELBusBreak.NEW().node(info).toELWrapper();break;
            case "script": el = ELBusThen.NEW().node(info).toELWrapper();break;
            case "switch_script": el = ELBusSwitch.NEW().node(info).toELWrapper();break;
            case "if_script": el = ELBusIf.NEW().node(info).toELWrapper();break;
            case "for_script": el = ELBusFor.NEW().node(info).toELWrapper();break;
            case "while_script": el = ELBusWhile.NEW().node(info).toELWrapper();break;
            case "break_script": el = ELBusBreak.NEW().node(info).toELWrapper();break;
            case "fallback":
            default:
        }
        return el;
    }

    public static String toEL(NodeInfo info, boolean format){
        handler(info);
        String el = null;
        switch (info.getType()){
            case "common": el = ELBusThen.NEW().node(info).toEL(format);break;
            case "switch": el = ELBusSwitch.NEW().node(info).toEL(format);break;
            case "if": el = ELBusIf.NEW().node(info).toEL(format);break;
            case "for": el = ELBusFor.NEW().node(info).toEL(format);break;
            case "while": el = ELBusWhile.NEW().node(info).toEL(format);break;
            case "iterator": el = ELBusIterator.NEW().node(info).toEL(format);break;
            case "break": el = ELBusBreak.NEW().node(info).toEL(format);break;
            case "script": el = ELBusThen.NEW().node(info).toEL(format);break;
            case "switch_script": el = ELBusSwitch.NEW().node(info).toEL(format);break;
            case "if_script": el = ELBusIf.NEW().node(info).toEL(format);break;
            case "for_script": el = ELBusFor.NEW().node(info).toEL(format);break;
            case "while_script": el = ELBusWhile.NEW().node(info).toEL(format);break;
            case "break_script": el = ELBusBreak.NEW().node(info).toEL(format);break;
            case "fallback":
            default:
        }
        return el;
    }

    public static void handler(NodeInfo info) {
        if(StrUtil.isEmpty(info.getScript())){info.setScript(null);}
        if(StrUtil.isEmpty(info.getLanguage())){info.setLanguage(null);}
        if(StrUtil.isEmpty(info.getClazz())){info.setClazz(null);}
        if(StrUtil.isEmpty(info.getCmpPre())){info.setCmpPre(null);}
        if(StrUtil.isEmpty(info.getCmpFinallyOpt())){info.setCmpFinallyOpt(null);}
        if(StrUtil.isEmpty(info.getCmpId())){info.setCmpId(null);}
        if(StrUtil.isEmpty(info.getCmpTag())){info.setCmpTag(null);}
        if(StrUtil.isEmpty(info.getCmpTo())){info.setCmpTo(null);}
        if(StrUtil.isEmpty(info.getCmpDefaultOpt())){info.setCmpDefaultOpt(null);}
        if(StrUtil.isEmpty(info.getCmpTrueOpt())){info.setCmpTrueOpt(null);}
        if(StrUtil.isEmpty(info.getCmpFalseOpt())){info.setCmpFalseOpt(null);}
        if(StrUtil.isEmpty(info.getCmpDoOpt())){info.setCmpDoOpt(null);}
        if(StrUtil.isEmpty(info.getCmpBreakOpt())){info.setCmpBreakOpt(null);}
    }

}
